/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

export let SysApp = {
    url: {},
    datas: {}
};

export function getXhrMessage ( xhr ){
    if( !xhr || !xhr.responseText ){
        return '未解析错误!';
    }
    var errInfoObj = JSON.parse( xhr.responseText );
    if( errInfoObj && errInfoObj.errCode ){
        return errInfoObj.errCode + errInfoObj.errMessage;
    }else if( errInfoObj && errInfoObj.message ){
        return errInfoObj.message;
    }else{
        return xhr.responseText;
    }
    
}

export function getDataForSP ( pageData ){
    return Object.assign({
        page: pageData.currentPage - 1,
        size: pageData.pageSize
    }, pageData.searchData );
}

export function writePageInfo ( vmData, pageData ){
    if( pageData.page ){
        vmData.pageSize = pageData.page.size;
        vmData.totalElements = pageData.page.totalElements;
    }else{
        vmData.pageSize = pageData.size;
        vmData.totalElements = pageData.totalElements;
    }
}

export function convertMapToArray ( objMap ){
    if( isArrayFn( objMap ) ){
        return objMap;
    }
    var reAs = [];
    for( var p in objMap ){
        reAs.push( objMap[p] );
    }
    return reAs;
}

export function isArrayFn ( value ){
    if (typeof Array.isArray === "function") {
        return Array.isArray( value );
    }else{
        return Object.prototype.toString.call( value ) === "[object Array]";
    }
}

export function isObjectFn ( value ){
    return Object.prototype.toString.call( value ) === "[object Object]";
}

export function moveForEmbedded ( obj, assName, assValue ){
    if( obj._embedded && obj._embedded[assName] ){
        obj[assName] = obj._embedded[assName];
    }else{
        obj[assName] = assValue;
    }
    return obj;
}

export function findNode ( nds, key, assName ){
    var cn = null;
    for( var p in nds ){
        if( nds[p].id === key ){
            cn = nds[p];
            break;
        }
    }
    if( cn ){
        return cn;
    }
    for( var p in nds ){
        cn = findNode( nds[p][assName], key, assName );
        if( cn ){
            return cn;
        }
    }
    return cn;
}
export function removeNode ( nds, key, assName ){
    var tp = -1;
    for( var p in nds ){
        if( nds[p].id === key ){
            tp = p;
            break;
        }
    }
    if( tp > -1 ){
        nds.splice( tp, 1 );
        return;
    }
    for( var p in nds ){
        if( nds[p][assName] ){
            removeNode( nds[p][assName], key, assName );
        }
    }
}

export function replaceNode ( pn, cn, assName ){
    if( !pn || !cn || !pn[assName] ){
        return;
    }
    var tcn = null;
    var tp = -1;
    for( var p in pn[assName] ){
        if( pn[assName][p].id === cn.id ){
            tcn = pn[assName][p];
            tp = p;
        }
    }
    if( !tcn || tp < 0 ){
        return;
    }
    pn[assName].splice( tp, 1, cn );
}

//根据参数名获取传参的值
export function getQueryString ( name ) {
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if( r !== null ){
        return unescape( r[2] );
    }      
    return null;
}

export function formatObjAndCopy( obj ){
    var b = Object.assign( {}, obj );
    delete b._links;
    delete b.uuid;
    delete b.createTime;
    delete b.updateTime;
    delete b.version;
    //移除所有数组
    for( var p in b ){
        if( isArrayFn( b[p] ) ){
            delete b[p];
        }
    }
    return b;
}

export function formatObjTree( obj, arr ){
    for( let p in obj ){
        if( isArrayFn( obj[p] ) || isObjectFn( obj[p] ) ){
            //递归函数
            let no = {};
            arr.push( no );
            no.label = p;
            no.children = [];
            formatObjTree( obj[p], no.children );
        }else{
            arr.push({
                label: p + "=" + obj[p]
            });
        }
    }
}

export function errMsg( error, _this, msg ) {
    if (error.response && error.response.data.code === '[kwc-0002]') {
        _this.$message({
            message: msg,
            type: 'warning'
        });
    } else if (error.response && error.response.data.code === '[kwc-0006]') {
        _this.$message({
            message: msg,
            type: 'warning'
        });
    }
}

export function setCookie( c_name, value, expiredays ) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

export function getCookie( c_name ) {
    if (document.cookie.length>0) {
        var  c_start=document.cookie.indexOf(c_name + "=");
        if (c_start !== -1) {
            c_start=c_start + c_name.length+1;
            var c_end=document.cookie.indexOf(";",c_start);
            if (c_end === -1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end))
        }
    }
    return "";
}

//去除首尾空格
export function replaceSpaces( val ) {
    return val.replace(/(^\s*)|(\s*$)/g, "");
}
//判断是否为String类型
export function isString( str ){
    return (typeof str == 'string') && str.constructor === String ;
}
//为一个对象移除首尾空格
export function replaceSpacesObj( obj ){
    for (let o in obj) {
        if ( isString(obj[o]) && ( obj[o] !== null ) ) {
            obj[o] = replaceSpaces(obj[o]);
        }
    }
    return obj ;
}
//对比字典表CODE值
export function replaceOneDictionarCode(allCode,myCode){
  if(myCode!=null&&myCode!=''){
    var replaceCodevalue=''
    for(let r in allCode){
      if(myCode==allCode[r].code){
        replaceCodevalue = allCode[r].name
      }
    }
    return replaceCodevalue
  }
}
export function replaceListDictionarCode(allCode,myCode){
  var replaceCodevalue = []
  if(myCode.length > 0){
    for(let r in allCode){
      for(let i in myCode){
        if(myCode[i]==allCode[r].code){
          replaceCodevalue.push(allCode[r].name)
        }
      }
    }
    return replaceCodevalue + ''
  }
}
//查询字典列表
export function replaceDictionart(dictionartCode){
  this.axios
    .request({
      method: 'get',
      url: './kwp/kdi/para/code/'+dictionartCode,
      params: {}
    })
    .then(response => {
    return response.data.subParas
  })
.catch(error => {
    console.log(error)
  })
}
